package com.mrper.ext.property;

import android.annotation.SuppressLint;
import android.databinding.BindingAdapter;
import android.text.TextUtils;

import com.bm.library.PhotoView;
import com.miguo.app.AppContext;
import com.miguo.framework.view.LazyImageView;
import com.miguo.http.Url;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * Created by admin on 2016/3/11.
 * ImageView属性扩展
 */
public class ImageViewExt {

    /**
     * 加载图片资源
     * @param imgview
     * @param imgUrl
     * @param displayOption
     */
    @SuppressWarnings("all")
    @SuppressLint("all")
    @BindingAdapter({"bind:imgUrl","bind:displayOption"})
    public static void loadImage(LazyImageView imgview, String imgUrl, DisplayImageOptions displayOption){
        try {
            if(!TextUtils.isEmpty(imgUrl)) {
                boolean isFullUrl = imgUrl.toLowerCase().startsWith("http://") || imgUrl.toLowerCase().startsWith("https://");
                String resultUrl = isFullUrl ? imgUrl : Url.API_URL + imgUrl;
                imgview.setNoImgMode(AppContext.isNoImg);
                imgview.autoDisplayImage(resultUrl, displayOption);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 加载图片资源
     * @param imgview
     * @param imgUrl
     * @param displayOption
     */
    @SuppressWarnings("all")
    @SuppressLint("all")
    @BindingAdapter({"bind:imgUrl","bind:displayOption"})
    public static void loadImage(PhotoView imgview,String imgUrl,DisplayImageOptions displayOption){
        try {
            if(!TextUtils.isEmpty(imgUrl)) {
                boolean isFullUrl = imgUrl.toLowerCase().startsWith("http://") || imgUrl.toLowerCase().startsWith("https://");
                String resultUrl = isFullUrl ? imgUrl : Url.API_URL + imgUrl;
                if(!AppContext.isNoImg) //软件是否开了不显示图片功能
                    ImageLoader.getInstance().displayImage(resultUrl, imgview, displayOption);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
